﻿using Corina.Commands;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Ink;
using System.Windows.Input;

namespace Corina.Cydee.Apphub.Viewmodels
{
    public class DevicekindViewmodel : ViewmodelBase
    {
        #region Constructor
        public DevicekindViewmodel()
        {
            DeviceClient.GetDeviceKindItemsCompleted += GetDeviceKindItemsCompleted;
            DeviceClient.GetDeviceItemsCompleted += GetDeviceItemsCompleted;
            DeviceClient.InsertDeviceKindCompleted += InsertDeviceKindCompleted;
            DeviceClient.RemoveDeviceKindCompleted += RemoveDeviceKindCompleted;
            DeviceClient.UpdateDeviceKindCompleted += UpdateDeviceKindCompleted;

            if (DataContext.IsValid)
            {
                LoadData();
            }
        }

        #endregion

        #region Methods
        private void UpdateDeviceKindCompleted(object sender, DataService.UpdateDeviceKindCompletedEventArgs e)
        {
            UpdateEntity = new DeviceKindInfo();
            Operation = EntityOperation.None;
            LoadData();
        }

        private void RemoveDeviceKindCompleted(object sender, DataService.RemoveDeviceKindCompletedEventArgs e)
        {
            LoadData();
        }

        private void GetDeviceItemsCompleted(object sender, DataService.GetDeviceItemsCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                
                var items = (from t in e.Result.DataObjects
                             select new
                             {
                                 DeviceSn = (string)t["DeviceSn"]
                             });
                DeviceCollection = new ObservableCollection<object>(items);
                DeviceCount = e.Result.TotalCount;
            }
            else
            {
                DeviceCount = 0;
                MessageBox.Show(e.Error.Message);
            }
        }

        private void InsertDeviceKindCompleted(object sender, DataService.InsertDeviceKindCompletedEventArgs e)
        {
            
            Operation = EntityOperation.None;
            UpdateEntity = new DeviceKindInfo();
            LoadData();
        }

        private void GetDeviceKindItemsCompleted(object sender, DataService.GetDeviceKindItemsCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                DeviceKindCollection = new ObservableCollection<DeviceKindInfo>(e.Result.Entities.OfType<DeviceKindInfo>());
            }
            else
            {
                MessageBox.Show(e.Error.Message);
            }
        }

        protected override void LoadData(params object[] paras)
        {
            DeviceClient.GetDeviceKindItemsAsync();
        } 
        #endregion
        
        #region Collection
        private ObservableCollection<object> _deviceCollection;
        public ObservableCollection<object> DeviceCollection
        {
            get { return _deviceCollection; }
            set
            {
                _deviceCollection = value;
                RaisePropertyChanged(() => DeviceCollection);
            }
        }

        private int _deviceCount = 0;
        public int DeviceCount
        {
            get { return _deviceCount; }
            set
            {
                _deviceCount = value;
                RaisePropertyChanged(() => DeviceCount);
            }
        }

        private ObservableCollection<DeviceKindInfo> _deviceKindCollection;
        public ObservableCollection<DeviceKindInfo> DeviceKindCollection
        {
            get { return _deviceKindCollection; }
            set
            {
                _deviceKindCollection = value;
                RaisePropertyChanged(() => DeviceKindCollection);
                RaisePropertyChanged(() => DisplayCollection);
            }
        }

        private DeviceKindInfo _selectedDeviceKindItem;
        public DeviceKindInfo SelectedDeviceKindItem
        {
            get { return _selectedDeviceKindItem; }
            set
            {
                _selectedDeviceKindItem = value;
                RaisePropertyChanged(() => SelectedDeviceKindItem);
                if (SelectedDeviceKindItem != null)
                {
                    DeviceClient.GetDeviceItemsAsync(SelectedDeviceKindItem.Id, "", 0, 1, 10);
                }
                else
                {
                    DeviceCollection = null;
                }
            }
        }

        public IEnumerable<DeviceKindInfo> DisplayCollection
        {
            get
            {
                return DeviceKindCollection == null ? null : DeviceKindCollection.Where(t => t.Name.Contains(Keywords) || t.Description.Contains(Keywords));
            }
        }

        private string _keywords = string.Empty;
        public string Keywords
        {
            get { return _keywords; }
            set
            {
                _keywords = value;
                RaisePropertyChanged(() => Keywords);
                RaisePropertyChanged(() => DisplayCollection);
            }
        }
        #endregion

        #region Properties
        private bool _showDeviceList = true;
        public bool ShowDeviceList
        {
            get { return _showDeviceList; }
            set
            {
                _showDeviceList = value;
                RaisePropertyChanged(() => ShowDeviceList);
            }
        }

        private DeviceKindInfo _updateEntity = new DeviceKindInfo();
        public DeviceKindInfo UpdateEntity
        {
            get { return _updateEntity; }
            set
            {
                _updateEntity = value;
                RaisePropertyChanged(() => UpdateEntity);
            }
        }

        #endregion

        #region Commands
        public ICommand InsertCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate {
                        UpdateEntity = new DeviceKindInfo();
                        Operation = EntityOperation.Insert;
                        
                    }
                };
            }
        }

        public ICommand UpdateCommand
        {
            get
            {
                return new DelegateCommand(t =>
                {
                    int id = Convert.ToInt32(t);

                    UpdateEntity = DeviceKindCollection.FirstOrDefault(i => i.Id == id);
                    Operation = EntityOperation.Update;
                    
                });
            }
        }

        public ICommand RemoveCommand
        {
            get
            {
                return new DelegateCommand(t =>
                {
                    int id = Convert.ToInt32(t);

                    if(ShowConfirm(Language.LanguageKeys.IDS_DELETE_CONFIRM) == MessageBoxResult.OK)
                    {
                        DeviceClient.RemoveDeviceKindAsync(Account, Password, id);
                    }

                });
            }
        }

        public ICommand CancelCommand
        {
            get {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate {
                        
                        Operation = EntityOperation.None;
                        UpdateEntity = new DeviceKindInfo();
                    }
                };
            }
        }

        public ICommand SubmitCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        if (Operation == EntityOperation.Insert)
                        {
                            DeviceClient.InsertDeviceKindAsync(Account, Password, UpdateEntity.Name, UpdateEntity.Description);
                        }
                        else if(Operation == EntityOperation.Update)
                        {
                            DeviceClient.UpdateDeviceKindAsync(Account, Password, UpdateEntity.Name, UpdateEntity.Description, UpdateEntity.Id);
                        }
                    }
                };
            }
        }
        #endregion
    }
}
